<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>事件循环加监听</title>
</head>
<body>
    <button>按钮1</button>
    <button>按钮2</button>
    <button>按钮3</button>

  <script>
  /**
   * 下面这种方式会出问题因为事件中操作的i是同一个i
  */

   /*    var btns =document.getElementsByTagName("button");
      for(var i =0;i<btns.length;i++){
          btns[i].onclick = function(){
              console.log(`我是第${i}个按钮`);
          }
      } */
      var btns =document.getElementsByTagName("button");


      
      for(var i =0;i<btns.length;i++){
            (function(i){
                btns[i].onclick = function(){
              console.log(`我是第${i}个按钮`);
          }})(i)
          
      } 



  </script>



</body>
</html>